package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.SubstationModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 变电站业务
 * 
 * @author zhaokf
 * @date 2013-04-19
 */
public class SubstationService extends BaseService
{
	/**
	 * 分页列出变电站
	 * 
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<SubstationModel> list(Map<String, String> params)
	{
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try
		{
			limit = params.get("limit") == null ? 0 : Integer.parseInt(params
					.get("limit").toString());
			page = params.get("page") == null ? 1 : Integer.parseInt(params
					.get("page").toString());
		}
		catch (Exception e)
		{
			limit = 100;
			page = 1;
		}
		String where = "";
		try
		{
			if (search.get("subs_no") != null)
			{
				where = " AND subs_no=" + search.get("subs_no");
			}
			if (search.get("subs_name") != null)
			{
				where = " AND subs_name LIKE '%" + search.get("subs_name")
						+ "%'";
			}
			where = " WHERE " + where.substring(5);
		}
		catch (Exception e)
		{
		}
		Page<SubstationModel> Subs = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM G_SUBS " + where;

		try
		{
			Subs = SubstationModel.dao.paginate(page, limit, select_sql,
					query_sql);
		}
		catch (Exception e)
		{
			setError(e.getMessage());
		}
		return Subs;
	}

	/**
	 * 增加变电器信息
	 * 
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String, String> params)
	{

//		String no = params.get("subs_code") == null ? "" : params.get("subs_code").toString();
//		String name = params.get("subs_name") == null ? "" : params.get("subs_name").toString();
//		String org = params.get("subs_org_no") == null ? "" : params.get("subs_org_no").toString();
//		String volt = params.get("subs_volt") == null ? "" : params.get("subs_volt").toString();
//		String addr = params.get("subs_addr") == null ? "" : params.get("subs_addr").toString();
//		String cnt = params.get("subs_mt_cnt") == null ? "" : params.get("subs_mt_cnt").toString();
//		String cap = params.get("subs_mt_cap") == null ? "" : params.get("subs_mt_cap").toString();
//		String inLine = params.get("subs_inLine_no") == null ? "" : params.get("subs_inLine_no").toString();
//		String priv = params.get("subs_status") == null ? "" : params.get("subs_status").toString();
//		String status = params.get("subs_priv_code") == null ? "" : params.get("subs_priv_code").toString();
//
//		String sql = "select * from g_subs where subs_code=?";
//		SubstationModel sm = SubstationModel.dao.findFirst(sql, no);
//		if (sm == null)
//		{
//			sm = new SubstationModel();
//			sm.set("SUBS_ID", Db.queryBigDecimal("select g_subs_seq.nextval from dual").intValue());
//			if (!"".equals(no)){sm.set("SUBS_CODE", no);}
//			if(!"".equals(name)){sm.set("SUBS_NAME", name);}
//			if(!"".equals(org)){sm.set("SUBS_ORG_NO", org);}
//			if(!"".equals(volt)){sm.set("subs_volt", volt);}
//			if(!"".equals(addr)){sm.set("subs_addr", addr);}
//			if(!"".equals(cnt)){sm.set("subs_mt_cnt", cnt);}
//			if(!"".equals(cap)){sm.set("subs_mt_cap", cap);}
//			if(!"".equals(inLine)){sm.set("subs_inLine_no", inLine);}
//			if(!"".equals(priv)){sm.set("subs_status", priv);}
//			if(!"".equals(status)){sm.set("subs_priv_code", status);}
//			sm.save();
//			return true;
//		}
//		return setError("你访问的变电器已存在！");
		Record record = new Record();
		record.set("subs_id", Db.queryBigDecimal("select g_subs_seq.nextval from dual").intValue());
		record.set("subs_name", params.get("subs_name").toString());
		record.set("subs_org_no", params.get("subs_org_no").toString());
		record.set("subs_code", params.get("subs_code").toString());
		boolean isupadded = Db.save("g_subs", "subs_id",record);
		if(isupadded){
			return true;
		}
		return setError("保存变电站信息失败！");
	}
	/**
	 * 根据编号查询变电站
	 * @param no
	 * @return
	 */
	public static SubstationModel isGet(Map<String, String> params)
	{
		SubstationModel sm = null;
		String no = params.get("subs_code") == null ? "" : params.get("subs_code").toString();
		String sql = "select A.*,B.org_name,C.line_name from g_subs A,sa_org B,g_line C where A.subs_org_no=B.org_no and A.subs_inLine_no=C.line_id and subs_code=?";
		try{
		 sm = SubstationModel.dao.findFirst(sql, no);
		}catch(Exception e){
			setError(e.getMessage());
		}
		return sm;
	}

	/**
	 * 修改变电站信息
	 * 
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String, String> params)
	{
//		String id = params.get("subs_id") == null ? "" : params.get("subs_id").toString();
//		String no = params.get("subs_code") == null ? "" : params.get("subs_code").toString();
//		String name = params.get("subs_name") == null ? "" : params.get("subs_name").toString();
//		String org = params.get("subs_org_no") == null ? "" : params.get("subs_org_no").toString();
//		String volt = params.get("subs_volt") == null ? "" : params.get("subs_volt").toString();
//		String addr = params.get("subs_addr") == null ? "" : params.get("subs_addr").toString();
//		String cnt = params.get("subs_mt_cnt") == null ? "" : params.get("subs_mt_cnt").toString();
//		String cap = params.get("subs_mt_cap") == null ? "" : params.get("subs_mt_cap").toString();
//		String inLine = params.get("subs_inLine_no") == null ? "" : params.get("subs_inLine_no").toString();
//		String priv = params.get("subs_status") == null ? "" : params.get("subs_status").toString();
//		String status = params.get("subs_priv_code") == null ? "" : params.get("subs_priv_code").toString();
//		SubstationModel sm = SubstationModel.dao.findById(id);
//		if (sm != null)
//		{
//			if ("".equals(no)){sm.set("subs_code", no);}
//			if("".equals(name)){sm.set("subs_name", name);}
//			if("".equals(org)){sm.set("subs_org_no", org);}
//			if("".equals(volt)){sm.set("subs_volt", volt);}
//			if("".equals(addr)){sm.set("subs_addr", addr);}
//			if("".equals(cnt)){sm.set("subs_mt_cnt", Integer.parseInt(cnt));}
//			if("".equals(cap)){sm.set("subs_mt_cap", Integer.parseInt(cap));}
//			if("".equals(inLine)){sm.set("subs_inLine_no", inLine);}
//			if("".equals(priv)){sm.set("subs_status", priv);}
//			if("".equals(status)){sm.set("subs_priv_code", Integer.parseInt(status));}
//			sm.update();
//			return true;
//		}
		Record record = new Record();
		record.set("subs_id", params.get("subs_id").toString());
		record.set("subs_name", params.get("subs_name").toString());
		record.set("subs_org_no", params.get("subs_org_no").toString());
		record.set("subs_code", params.get("subs_code").toString());
		boolean isupadded = Db.update("g_subs", "subs_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的变电器不存在！");
	}

	/**
	 * 删除变电站信息
	 * 
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String, String> params)
	{
		String no = params.get("subs_code") == null ? "" : params.get("subs_code").toString();
		String sql = "select * from g_subs where subs_code=?";
		SubstationModel sm = SubstationModel.dao.findFirst(sql, no);
		if (sm != null)
		{
			sm.delete();
			return true;
		}
		return setError("您访问的变电站不存在！");
	}
}
